// created Jan 16, 2009 10:25:18 AM
// by petrovic
/*
Copyright 2009 Homosuperiorus LLC 

Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.

*/

package org.taglets.pub;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.protocol.Protocol;
import org.taglets.exception.TagletsException;
import org.taglets.pub.account.*;
import org.taglets.pub.tags.*;
import org.taglets.ssl.EasySSLProtocolSocketFactory;
import org.taglets.util.Util;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

public abstract class Client {
    @SuppressWarnings("unused")
    private final Logger LOG = Logger.getLogger(Client.class.getName());

    protected static String host;
    protected static int port = 443;
    protected ApiUrl apiUrl;
    protected Map<String, String> form = new HashMap<String, String>();
    static HttpClient httpClient;

    static {
        Properties hostProperties = Util.readPropertiesAsResource("host.properties");
        host = hostProperties.getProperty("host", "taglets0.appspot.com");
        Protocol https = new Protocol("https", new EasySSLProtocolSocketFactory(), port);
        httpClient = new HttpClient();
        httpClient.getHostConfiguration().setHost(host, port, https);
    }

    private HttpResponse execute(String method) throws IOException, URISyntaxException {
        URI uri = new URI(apiUrl.getResolved());
        HttpMethodBase httpMethod;
        if (method.equalsIgnoreCase("post")) {
            httpMethod = new PostMethod(uri.toString());
            String body = Util.urlEncode(form);
            RequestEntity entity = new StringRequestEntity(body, "application/x-www-form-urlencoded", "UTF-8");
            ((PostMethod) httpMethod).setRequestEntity(entity);
        } else {
            httpMethod = new GetMethod(uri.toString());
        }

        httpMethod.setRequestHeader("User-Agent", "taglets.org; Java client reference impl");
        httpClient.executeMethod(httpMethod);
        return new HttpResponse(httpMethod);
    }

    protected AppResponse post() throws IOException, URISyntaxException, TagletsException, NoSuchMethodException {
        HttpResponse response = this.execute("POST");
        return appResponse(response);
    }

    protected AppResponse get() throws IOException, URISyntaxException, TagletsException, NoSuchMethodException {
        HttpResponse response = this.execute("GET");
        return appResponse(response);
    }

    private AppResponse appResponse(HttpResponse httpResponse) throws TagletsException, IOException, NoSuchMethodException {
        AppResponse appResponse;
        if (apiUrl.getPath().equals(Route.ACCOUNT_REGISTER)) {
            appResponse = new RegisterResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.ACCOUNT_CONFIRM)) {
            appResponse = new ConfirmationResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.ACCOUNT_REMIND)) {
            appResponse = new RemindResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.ACCOUNT_RETRIEVE)) {
            appResponse = new RetrieveResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.OUTLET_BIND)) {
            appResponse = new BindOutletResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_CREATE)) {
            appResponse = new TagCreateResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_COMMENT)) {
            appResponse = new TagCommentResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_FOLLOW)) {
            appResponse = new TagFollowResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_LIST)) {
            appResponse = new TagListResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_MOSTCOMMENT)) {
            appResponse = new TagMostCommentResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_MOSTRECENT)) {
            appResponse = new TagMostRecentResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_RETRIEVE)) {
            appResponse = new TagRetrieveResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_STATUS)) {
            appResponse = new TagStatusResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_SEARCH)) {
            appResponse = new TagSearchResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_SEARCH)) {
            appResponse = new TagSearchResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TAG_UPDATE)) {
            appResponse = new TagUpdateResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TWITTER_VERIFY)) {
            appResponse = new VerifyTwitterResponse(httpResponse);
        } else if (apiUrl.getPath().equals(Route.TWITTER_CONFIRM)) {
            appResponse = new ConfirmTwitterResponse(httpResponse);
        } else {
            throw new NoSuchMethodException();
        }
        return appResponse;
    }

    /*
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Generic client: ").append(protocol).append(host);
        return sb.toString();
    }
    */

}